﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Print.Domain;
using Mengsk.ProxyDelivery.Print.Domain.DeliveryNumberGenerator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace Mengsk.ProxyDelivery.Print.UI
{
    class DeliveryNumberValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }

            PrintTemplate template = value as PrintTemplate;
            if (template == null)
            {
                MessageBox.Show("IDeliveryNumberValueConverter::Convert 传参数不正确");
                return "";
            }

            try
            {
                if (Mengsk.ProxyDelivery.Print.Domain.SystemConfig.Instance.LastDeliveryNumber.ContainsKey(template.Name) && string.IsNullOrWhiteSpace(Mengsk.ProxyDelivery.Print.Domain.SystemConfig.Instance.LastDeliveryNumber[template.Name]) == false)
                {
                    return DeliveryNumberGeneratorManager.GenenrateNext(template.DeliveryCompany.Title, Mengsk.ProxyDelivery.Print.Domain.SystemConfig.Instance.LastDeliveryNumber[template.Name]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
